package com.design.oop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.design.oop.bean.WheatBeerDegreeComparator;
import com.design.oop.bean.WheatBeerKind;
import com.design.oop.bean.WheatBeerKind.SortType;
import com.design.oop.bean.WheatBeerRateComparator;
import com.design.oop.util.BookmarkPreference;
import com.design.oop.util.CommonUtil;
import com.design.oop.util.NToast;

public class WheatBeerKindActivity extends Activity {
	private GridView mGridView;
	private static Toast toast;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.beer_kind);

		Bundle bundle = getIntent().getExtras();
		ArrayList<WheatBeerKind> beerList = new ArrayList<WheatBeerKind>();

		if (bundle != null && bundle.containsKey("bookmark")) {
			if (bundle.getBoolean("bookmark", false)) {
				Set<Integer> bookmarkSet = BookmarkPreference.getBookmarkList(this);
				if (CommonUtil.isEmptySet(bookmarkSet)) {
					NToast.showToast(toast, WheatBeerKindActivity.this, "즐겨찾기 한 맥주가 없습니다.", true);
					finish();
					return;
				}

				for (int bookmark : bookmarkSet) {
					beerList.add(WheatBeerKind.BEER_LIST.get(bookmark));
				}
			} else {
				beerList.addAll(WheatBeerKind.BEER_LIST);
			}
		} else {
			beerList.addAll(WheatBeerKind.BEER_LIST);
		}

		SortType sortType = SortType.ENAME;
		if (bundle != null && bundle.containsKey("sortType")) {
			int sortTypeValue = bundle.getInt("sortType");
			sortType = SortType.of(sortTypeValue);
			if (SortType.DEGREE == sortType) {
				Collections.sort(beerList, new WheatBeerDegreeComparator());
			} else if (SortType.RATE == sortType) {
				Collections.sort(beerList, new WheatBeerRateComparator());
			}
		}

		mGridView = (GridView)findViewById(R.id.beer_kind_grid_view);
		BeerGridAdapter gridAdapter = new BeerGridAdapter(this, R.layout.grid_item_row, beerList, sortType);
		mGridView.setAdapter(gridAdapter);
	}

	private class BeerGridAdapter extends ArrayAdapter<WheatBeerKind> implements OnClickListener {
		private ArrayList<WheatBeerKind> items;
		private LayoutInflater mInflater;
		private Context mContext;
		private int mResourceId;
		private SortType sortType;
		Handler handler = new Handler();

		public BeerGridAdapter(Context context, int textViewResourceId, ArrayList<WheatBeerKind> items, SortType sortType) {
			super(context, textViewResourceId, items);
			this.mResourceId = textViewResourceId;
			this.items = (ArrayList<WheatBeerKind>)items;
			this.mInflater = LayoutInflater.from(context);
			this.mContext = context;
			this.sortType = sortType;
		}

		@Override
		public WheatBeerKind getItem(int position) {
			return items.get(position);
		}

		@Override
		public int getCount() {
			if (CommonUtil.isEmptyList(items)) {
				return 0;
			}
			return items.size();
		}

		/**
		 * @param index
		 * @return
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int index) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(mResourceId, null);

				holder = new ViewHolder();
				holder.imageView = (ImageView)convertView.findViewById(R.id.grid_item_row_image);
				holder.imageView.setOnClickListener(BeerGridAdapter.this);
				holder.textView = (TextView)convertView.findViewById(R.id.grid_item_row_text);
				holder.addedTextView = (TextView)convertView.findViewById(R.id.grid_item_row_added_text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder)convertView.getTag();
			}

			final WheatBeerKind beer = items.get(position);
			if (beer != null) {
				holder.textView.setText(beer.getKoreanName());
				holder.imageView.setTag(beer.getDrawableId());
				handler.post(new Runnable() {
					@Override
					public void run() {
						holder.imageView.setImageResource(beer.getDrawableId());
					}
				});
				if (sortType == SortType.DEGREE) {
					holder.addedTextView.setVisibility(View.VISIBLE);
					holder.addedTextView.setText(beer.getDegree() + "도");
				} else if (sortType == SortType.RATE) {
					holder.addedTextView.setVisibility(View.VISIBLE);
					holder.addedTextView.setText(beer.getRate() + "/5.0");
				}
			}

			return convertView;
		}

		public class ViewHolder {
			public ImageView imageView;
			public TextView textView;
			public TextView addedTextView;
		}

		@Override
		public void onClick(View view) {
			int id = view.getId();
			if (id == R.id.grid_item_row_image) {
				int viewTag = (Integer)view.getTag();
				Intent intent = new Intent(mContext, WheatBeerDetailActivity_.class);
				intent.putExtra("id", viewTag);
				startActivity(intent);
			}
		}
	}
}
